package com.wkq.controller;

import com.wkq.domain.SysLog;
import com.wkq.service.ILogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;

@Component
@Aspect
public class LogAop {

    @Autowired
    private HttpServletRequest request;
    @Autowired
    private ILogService iLogService;

    private Date visitTime;//开始时间
    private Class aClass;//访问的类
    private Method method;//访问的方法

//    前置通知，主要是获取开始时间，执行的类是哪一个，执行的是哪一个方法
    @Before("execution(* com.wkq.controller.*.*(..))")
//    @Before("execution(* *..controller.*.*(..))")
    public void doBefore (JoinPoint jp) throws NoSuchMethodException {
        System.out.println("***************" + "前置通知");
        System.out.println("***************" + "前置通知");
        System.out.println("***************" + "前置通知");
        System.out.println("***************" + "前置通知");
        System.out.println("***************" + "前置通知");
        visitTime = new Date();//获取开始执行的时间
        aClass = jp.getTarget().getClass();//获取访问的具体类
        String methodName = jp.getSignature().getName();//获取方法名
        Object[] args = jp.getArgs();//获取访问方法的参数

//        获取正在执行的方法
        if (args == null || args.length == 0) {
            method = aClass.getDeclaredMethod(methodName);
        } else {
            Class[] classArgs = new Class[args.length];
            for (int i = 0; i < args.length; i++) {
                classArgs[i] = args[i].getClass();
            }
            method = aClass.getDeclaredMethod(methodName, classArgs);
        }
    }

    @After("execution(* com.wkq.controller.*.*(..))")
    public void doAfter (JoinPoint jp) {
//        获取访问时长
        Long time = new Date().getTime() - visitTime.getTime();

//        获取url
        String url = "";
        if (aClass != null && method != null && aClass != LogAop.class && aClass != LogController.class ) {
//            获取类上面的RequestMapping
            RequestMapping classAnnotation = (RequestMapping) aClass.getAnnotation(RequestMapping.class);
            if (classAnnotation != null) {
                String[] classValue = classAnnotation.value();
//                获取方法上的RequestMapping
                RequestMapping methodAnnotation = method.getAnnotation(RequestMapping.class);
                if (methodAnnotation != null) {
                    String[] methodValue = methodAnnotation.value();
                    url = classValue[0] + methodValue[0];
                }
            }
        }

//        获取ip
        String ip = request.getRemoteAddr();

//        获取当前操作用户
        SecurityContext context = SecurityContextHolder.getContext();
        User user = (User) context.getAuthentication().getPrincipal();
        String username = user.getUsername();

//        将获取到的数据存储到SysLog对象中
        SysLog sysLog = new SysLog();
        sysLog.setVisitTime(visitTime);
        sysLog.setUsername(username);
        sysLog.setIp(ip);
        sysLog.setUrl(url);
        sysLog.setExecutionTime(time);
        sysLog.setMethod("[类名] " + aClass.getName() + "[方法名] " + method.getName());

//        调用service把相应数据存到数据库
        iLogService.saveLog(sysLog);
    }
}
